<template>
  <ych-display-text-tag v-if="displayByText && disabled" v-model="text"/>
  <a-input
    v-else
    :disabled="disabled"
    :placeholder="placeholder || $t('m.eform.formDesign.label.placeholder_input')"
    :readOnly="readOnly"
    v-model="text"
  />
</template>

<script>

  import YchConstant from '@/api/YchConstant'
  import { putAction } from '@/api/manage'
  import YchDisplayTextTag from "../../views/eform/modules/designer/packages/KFormItem/YchDisplayTextTag";

  export default {
    name: 'YchSerialno',
    components:{
      YchDisplayTextTag,
    },
    props: {
      value: String,
      disabled: Boolean,
      readOnly: Boolean,
      displayByText: Boolean,
      placeholder: {
        type: String,
      },
      ruleCode: String,
      form: {
        type: Object,
        default: () => {
        }
      },
      record: Object,
      property: String,
    },
    data() {
      return {
        text: this.value,
        url: {
          executeRuleByCode: YchConstant.PREFIX_SYSTEM + '/fillRule/executeRuleByCode/'
        }
      }
    },
    created() {
      if (!this.value && this.ruleCode && this.form) {
        const formData = this.form.getFieldsValue()
        putAction(this.url.executeRuleByCode + this.ruleCode, formData).then((res) => {
          if (res.success) {
            this.text = res.result
          } else {
            this.messageUtil.warning(res.message)
          }
        })
      }
    },
    methods: {},
    watch: {
      value(value) {
        this.text = value
      },
      text() {
        this.$emit('change', this.text)
        this.$emit('setFieldValue', this.property, this.text, this.record)
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    }
  }
</script>
